<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class GoodsWholesale extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_goods_wholesale';


    protected $append = [

        'range'

    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-01-29 17:49
     */
    public function getRangeAttr($value,$data){

        if(!empty($data['id'])){

            $sale_model = new Wholesale();

            $dis = [

                'pid'    => $data['id'],

                'status' => 1
            ];

            $info = $sale_model->where($dis)->field('min,max,discount,id')->order('min')->select()->toArray();

            return $info;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-01-19 10:44
     * @功能说明:查询
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }



    /**
     * @author chenniang
     * @DataTime: 2021-01-19 10:36
     * @功能说明:编辑
     */
   public function dataUpdate($dis,$data){

       $data['update_time'] = time();

       $range = $data['range'];

       unset($data['range']);

       $res = $this->where($dis)->update($data);

       $this->updateSome($dis['id'],$range,$data['uniacid']);

       return $res;

   }


    /**
     * @author chenniang
     * @DataTime: 2021-01-29 15:53
     * @功能说明:添加批发价规则
     */
   public function dataAdd($data){

       $data['create_time'] = time();

       $data['update_time'] = time();

       $data['status'] = 1;

       $range = $data['range'];

       unset($data['range']);

       $res = $this->insert($data);

       $id  = $this->getLastInsID();

       $this->updateSome($id,$range,$data['uniacid']);

       return $res;

   }


    /**
     * @author chenniang
     * @DataTime: 2021-01-29 16:34
     * @功能说明:编辑规则
     */
   public function updateSome($id,$range,$uniacid){

       $sale_model = new Wholesale();

       $sale_model->where(['pid'=>$id])->update(['status'=>-1]);

       if(!empty($range)){

           foreach ($range as $value){

               $insert = [

                   'uniacid' => $uniacid,

                   'pid'     => $id,

                   'min'     => $value['min'],

                   'max'     => $value['max'],

                   'discount'=> round($value['discount'],1),
               ];

               $sale_model->insert($insert);

           }

       }

       return true;

   }


    /**
     * @author chenniang
     * @DataTime: 2021-01-29 17:19
     * @功能说明:活动列表
     */
   public function dateList($dis,$page){

       $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

       return $data;

   }


    /**
     * @author chenniang
     * @DataTime: 2021-01-30 11:29
     * @功能说明:活动列表
     */
   public function dataList($dis,$page=10){

       $data = $this->alias('a')
               ->join('longbing_card_shop_spe_price b','a.spe_id = b.id')
               ->where($dis)
               ->field('a.*')
               ->group('a.id')
               ->order('a.id desc')
               ->paginate($page)
               ->toArray();

       return $data;

   }


    /**
     * @param $goods_id
     * @功能说明:根据商品id 查询低价活动
     * @author chenniang
     * @DataTime: 2021-01-30 11:51
     */
   public function wholesaleAtv($goods_id){

       $dis = [

           'a.status' => 1,

           'a.goods_id'=> $goods_id,

           'c.status' => 1,

           'b.status' => 1
       ];

       $data = $this->alias('a')
               ->join('longbing_card_shop_wholesale b','a.id = b.pid')
               ->join('longbing_card_shop_spe_price c','a.spe_id = c.id')
               ->where($dis)
               ->field('b.min,b.max,b.discount,c.id as spe_id')
               ->order('b.discount asc')
               ->find();

       $arr = '';

       if(!empty($data)){

           $data = $data->toArray();

           $arr = '选购'.$data['min'].'-'.$data['max'].'数量商品，享受折扣低至'.$data['discount'].'折';
       }

       return $arr;

   }


    /**
     * @param $goods_id
     * @功能说明:根据商品id 查询低价活动
     * @author chenniang
     * @DataTime: 2021-01-30 11:51
     */
    public function wholesaleAtvSelect($goods_id){

        $dis = [

            'a.status' => 1,

            'a.goods_id'=> $goods_id,

            'c.status' => 1,

        ];

        $data = $this->alias('a')
            ->join('longbing_card_shop_spe_price c','a.spe_id = c.id')
            ->where($dis)
            ->field('c.id as spe_id,a.id')
            ->select()
            ->toArray();

        $spe_model = new IndexShopSpePrice();

        if(!empty($data)){

            foreach ($data as &$v){

                $spe_info  = $spe_model->SpePriceFind(['id'=>$v['spe_id']]);

                $v['name'] = $spe_info['spe_name_text'];

                $dis = [

                    'pid' => $v['id'],

                    'status' => 1
                ];

                $list = Db::name('longbing_card_shop_wholesale')->where($dis)->select();

                if(!empty($list)){

                    foreach ($list as &$value){

                        $v['list'][] = '选购'.$value['min'].'-'.$value['max'].'数量商品，享受折扣'.$value['discount'].'折';

                    }

                }

            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-30 13:40
     * @功能说明:根据商品规格数量获取折扣
     */
   public function wholesaleDiscount($goods_id,$spe_id,$num,$uniacid){

       $dis = [

           'uniacid' => $uniacid,

           'goods_id'=> $goods_id,

           'spe_id'  => $spe_id,

           'status'  => 1
       ];

       $data = $this->dataInfo($dis);
       //默认折扣
       $arr['discount'] = 1;

       if(!empty($data['range'])){

           foreach ($data['range'] as $v){

               if($num>=$v['min']){

                   $discount = $v['discount'];

                   $arr['discount'] = $discount/10;

                   $arr['sale_id']  = $v['id'];
               }

           }

       }

       return $arr;
   }


    /**
     * @author chenniang
     * @DataTime: 2021-02-01 10:54
     * @功能说明:获取折扣
     */
   public function eventDiscount($where){

       $data = $this->wholesaleDiscount($where['goods_id'],$where['spe_id'],$where['number'],$where['uniacid']);

       $data['name'] = 'sale_count';

       $data['sale_discount'] = $data['discount'];

       return $data;

   }


















}